package com.linc.navtabviewpagerfragment.Activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.linc.navtabviewpagerfragment.Fragment.FirstFragment;
import com.linc.navtabviewpagerfragment.Fragment.RootFragment;
import com.linc.navtabviewpagerfragment.Fragment.SecondFragment;
import com.linc.navtabviewpagerfragment.Fragment.StaticFragment;
import com.linc.navtabviewpagerfragment.R;
import com.linc.navtabviewpagerfragment.View.ScrollTabView;


public class MainActivity extends AppCompatActivity {
    private final static String TAG = "MainActivity";
    static final int NAV_TAB_ITEMS = 4;

    ViewPager mPager;
    SlidePagerAdapter mPagerAdapter;
    private RadioGroup mRadioGroup;
    private RadioButton mRbWeibo;
    private ScrollTabView mScrollTabView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mScrollTabView = (ScrollTabView)findViewById(R.id.scroll_tab_view);
        mScrollTabView.setTabNum(NAV_TAB_ITEMS);
        mScrollTabView.setSelectedColor(getResources().getColor(R.color.orange_red),
                getResources().getColor(R.color.orange_red));
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new SlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        mPager.setOnPageChangeListener(new PageChangeListener());

        mRbWeibo = (RadioButton)findViewById(R.id.rb_weibo);

        mRadioGroup = (RadioGroup) findViewById(R.id.rg_tab);
        Log.d(TAG, "rb_weibo is " + R.id.rb_weibo);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Log.d(TAG, "on checked changed: " + checkedId);
                mPager.setCurrentItem(getPagePositionByCheckedId(checkedId));
            }
        });

        mRadioGroup.check(R.id.rb_weibo);
    }

    private int getPagePositionByCheckedId(int checkedId) {
        switch (checkedId) {
            case R.id.rb_weibo:
                return 0;
            case R.id.rb_at_me:
                return 1;
            case R.id.rb_discuss:
                return 2;
            case R.id.rb_mine:
                return 3;
            default:
                return 0;
        }
    }

    private int getCheckedIdByPagePosition(int position) {
        switch (position) {
            case 0:
                return R.id.rb_weibo;
            case 1:
                return R.id.rb_at_me;
            case 2:
                return R.id.rb_discuss;
            case 3:
                return R.id.rb_mine;
            default:
                return R.id.rb_weibo;
        }
    }

    public class SlidePagerAdapter extends FragmentPagerAdapter {
        public SlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Log.d(TAG,"getItem: "+position);
			/*
			 * IMPORTANT: This is the point. We create a RootFragment acting as
			 * a container for other fragments
			 */
            switch (position) {
                case 0:
                    return new RootFragment();
                case 1:
                    return new StaticFragment();
                case 2:
                    return new SecondFragment();
                case 3:
                    return new FirstFragment();
                default:
                    return new RootFragment();
            }
        }

        @Override
        public int getCount() {
            return NAV_TAB_ITEMS;
        }
    }

    private class PageChangeListener implements ViewPager.OnPageChangeListener{

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            Log.d(TAG, "onPageScrolled: position is " + position + ", positionOffset " + positionOffset +
                    ", positionOffsetPixels is " + positionOffsetPixels);
            mScrollTabView.setOffset(position,positionOffset);
        }

        @Override
        public void onPageSelected(int position) {
            Log.d(TAG,"onPageSelected: positon is "+position);
            mRadioGroup.check(getCheckedIdByPagePosition(position));
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            Log.d(TAG,"onPageScrollStateChanged: state is "+state);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
